package com.hp.ssoserver.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.lang.reflect.Field;
import java.util.Map;
import java.util.UUID;

/**
 * author: renping
 * date: 2020/7/24
 */
public class CommonUtil {

    private static final Logger logger = LoggerFactory.getLogger(CommonUtil.class);
    /**
     * 获取UUID主键
     *
     * @return
     */
    public static String uuid() {
        return UUID.randomUUID().toString().replaceAll("-", "");
    }

    /**
     * 修改 map 中空字符串为 null，其余字符串去掉前后空格
     *
     * @param map
     */
    public static void changeEmptyValToNull(Map<String, Object> map) {
        if (null != map && !map.isEmpty()) {
            for (String key : map.keySet()) {
                if (map.get(key) instanceof String) {
                    if (StringUtil.isBlank((String) map.get(key))) {
                        map.put(key, null);
                    } else {
                        map.put(key, map.get(key).toString().trim());
                    }
                }
            }
        }
    }

    /**
     * 设置对象中空字符串为null
     *
     * @param source
     * @param <T>
     * @return
     */
    public static <T> T setNullValue(T source) {
        if (null == source) {
            return source;
        }

        Field[] fields = source.getClass().getDeclaredFields();
        for (Field field : fields) {
            if (field.getGenericType().toString().equals(
                    "class java.lang.String")) {
                field.setAccessible(true);
                try {
                    Object obj = field.get(source);
                    if (StringUtil.isBlank((String) obj)) {
                        field.set(source, null);
                    } else if (obj != null) {
                        field.set(source, obj.toString().trim());
                    }
                } catch (IllegalAccessException e) {
                    logger.error(e.getMessage(), e);
                }
            }
        }
        return source;
    }

    /**
     * 获取加密的电话
     *
     * @param phone
     * @return
     */
    public static String getEncryPhone(String phone) {
        if (null != phone && phone.length() == 11) {
            return phone.substring(0, 3) + "****" + phone.substring(7);
        }

        return phone;
    }

    public static String getDownloadTempPath() {
        return System.getProperty("user.dir") + "/downloadTemp/";
    }

    public static void deleteDir(String dir) {
        try {
            File file = new File(dir);
            if (file.isDirectory()) {
                String[] children = file.list();
                if (children.length > 0) {
                    // 递归删除目录中的子目录下
                    for (int i = 0; i < children.length; i++) {
                        deleteDir(file.getPath() + "/" + children[i]);
                    }
                }
                if (!getDownloadTempPath().equals(dir)) {
                    file.delete();
                }
            } else {
                file.delete();
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
    }

    public static void main(String[] args) {
        System.out.println(uuid());
    }

}
